package org.evolution.util.db;

import java.io.Serializable;
import java.util.List;

/**
 * 数据对象，用于分页查询，支持泛型
 */
public class PageData<E> implements Serializable {

    private static final long serialVersionUID = 3618977884442868020L;

    public static int DEFAULT_ROWS_PER_PAGE = 30;
    
    private Serializable id;

    private int rowCount = -1;
    private int pageCount;
    private int rowsPerPage = DEFAULT_ROWS_PER_PAGE;
    private int currentPage = 1;

    private List<E> items;

    private Serializable userData;

    public String toString() {
        return new StringBuffer(64).append("rowCount:").append(rowCount)
                .append(";pageCount:").append(pageCount)
                .append(";rowsPerPage:").append(rowsPerPage).append(
                        ";currentPage:").append(currentPage).toString();
    }

    /**
     * 计算分页数。
     */
    public void count() {
        pageCount = (rowCount + rowsPerPage - 1) / rowsPerPage;
        pageCount = pageCount == 0 ? 1 : pageCount;
    }

    public void setItems(List<E> data) {
        this.items = data;
    }

    public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getRowCount() {
        return rowCount;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getRowsPerPage() {
        return rowsPerPage;
    }

    public void setRowsPerPage(int rowsPerPage) {
        this.rowsPerPage = rowsPerPage;
    }

    public List<E> getItems() {
        return items;
    }

    public Serializable getUserData() {
        return userData;
    }

    public void setUserData(Serializable userData) {
        this.userData = userData;
    }

    public Serializable getId() {
        return id;
    }

    public void setId(Serializable id) {
        this.id = id;
    }
}
